package cn.ad.server.web.spring.service.components.adtraffic2;

import cn.ad.server.web.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zooy on 17-1-12.
 */
public abstract class AbstractGDTDispatcher {

    private static Logger logger = LoggerFactory.getLogger(AbstractGDTDispatcher.class);

    protected String generateParam(Map<String,Object> map) {
        List<String> params = new ArrayList<>();
        if (map != null && map.size() > 0) {
            for (Map.Entry entry : map.entrySet()) {
                if (entry.getValue() instanceof String || entry.getValue() instanceof Integer) {
                    params.add(String.format("%s=%s", entry.getKey(), entry.getValue()));
                } else {
                    try {
                        params.add(String.format("%s=%s", entry.getKey(), URLEncoder.encode(JsonUtils.toJson(entry.getValue()),"utf-8")));
                    } catch (UnsupportedEncodingException e) {
                        logger.error("urlencode error");
                    }
                }
            }
        }

        return StringUtils.join(params, "&");
    }
}
